package com.mjfeed.activity;

import com.mjfeed.bean.MemberJSON;
import com.mjfeed.service.MemberService;
import com.mjfeed.util.Md5Util;
import com.mjfeed.util.NetUtils;
import com.mjfeed.util.SPUtils;
import com.mjfeed.widget.LoadingDialog;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 免费注册
 * @author zhaominglei
 * @date 2014-12-14
 * 
 */
public class RegisterActivity extends Activity implements OnClickListener,OnFocusChangeListener {
	@SuppressWarnings("unused")
	private static final String TAG = RegisterActivity.class.getSimpleName();
	
	private ImageView goHome;
	private EditText mobileText;
	private EditText passText;
	private EditText cfnpassText;
	private EditText emailText;
	private EditText nameText;
	private Button register;
	private LoadingDialog loadingDialog;
	private String postData;
	private Intent intent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		
		init();
	}
	
	private void init() {
		goHome = (ImageView) findViewById(R.id.register_gohome);
		mobileText = (EditText) findViewById(R.id.register_account_edt);
		passText = (EditText) findViewById(R.id.register_pass_edt);
		cfnpassText = (EditText) findViewById(R.id.register_cfnpass_edt);
		emailText = (EditText) findViewById(R.id.register_email_edt);
		nameText = (EditText) findViewById(R.id.register_name_edt);
		register = (Button) findViewById(R.id.register_register);
		
		goHome.setOnClickListener(this);
		register.setOnClickListener(this);
		mobileText.setOnFocusChangeListener(this); 
		cfnpassText.setOnFocusChangeListener(this);
		loadingDialog = new LoadingDialog(this);
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.register_gohome:
			finish();
			break;
		
		case R.id.register_register:
			register(v);
			break;
			
		default:
			break;
		}
	}

	/**
	 * 注册
	 */
	private void register(View v) {
		register.setClickable(false);
		String mobile = mobileText.getText().toString();
		String pass = passText.getText().toString();
		String cfnpass = cfnpassText.getText().toString();
		String email = emailText.getText().toString();
		String name = nameText.getText().toString();
		boolean hasCheck = true;
		if (mobile == null || mobile.equals("")) {
			hasCheck = false;
			Toast.makeText(v.getContext(), R.string.register_checkname_error1, Toast.LENGTH_SHORT).show();
		}
		if (pass == null || pass.equals("")) {
			hasCheck = false;
			Toast.makeText(v.getContext(), R.string.register_checkpass_error1, Toast.LENGTH_SHORT).show();
		}
		if (!cfnpass.equals(pass)) {
			hasCheck = false;
			Toast.makeText(v.getContext(), R.string.register_checkpass_error2, Toast.LENGTH_SHORT).show();
		}
		if (email == null || email.equals("")) {
			hasCheck = false;
			Toast.makeText(v.getContext(), R.string.register_checkemail_error1, Toast.LENGTH_SHORT).show();
		}
		
		if (hasCheck) {
			if (NetUtils.isConnected(getApplicationContext())) {
				loadingDialog.show();
				
				StringBuilder param = new StringBuilder();
				param.append("mobile=").append(mobile).append("&password=").append(Md5Util.encrypt(pass));
				param.append("&email=").append(email).append("&username=").append(mobile);
				param.append("&name=").append(name);
				postData = param.toString();
				new RegisterTask().execute("");
			} else {
				register.setClickable(true);
				Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
			}
		} else {
			register.setClickable(true);
		}
	}

	/**
	 * 失去焦点后校验
	 */
	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		switch (v.getId()) {
		case R.id.register_account_edt: 
			String mobile = mobileText.getText().toString();
			if (mobile == null || mobile.equals("")) {
				Toast.makeText(v.getContext(), R.string.register_checkname_error1, Toast.LENGTH_SHORT).show();
			}
			break;
			
		case R.id.register_cfnpass_edt: 
			String pass = passText.getText().toString();
			if (pass == null || pass.equals("")) {
				Toast.makeText(v.getContext(), R.string.register_checkpass_error1, Toast.LENGTH_SHORT).show();
			}
			break;
		
		case R.id.register_email_edt: 
			String email = emailText.getText().toString();
			if (email == null || email.equals("")) {
				Toast.makeText(v.getContext(), R.string.register_checkemail_error1, Toast.LENGTH_SHORT).show();
			}
			break;
			
		default:
			break;
		}
	}
	
	public class RegisterTask extends AsyncTask<String, String, MemberJSON> {
		
		@Override
		protected MemberJSON doInBackground(String... params) {
			return new MemberService().register(postData);
		}

		@Override
		protected void onPostExecute(MemberJSON result) {
			if (result == null) {
				Toast.makeText(getApplicationContext(), R.string.register_error, Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(getApplicationContext(), R.string.register_success, Toast.LENGTH_SHORT).show();
			}
			SPUtils.put(getApplicationContext(), "member", result);
			intent = new Intent(getApplicationContext(), MainActivity.class);
			startActivity(intent);
			RegisterActivity.this.finish();
			loadingDialog.dismiss();
		}
	}
}
